<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<!--edge浏览器H5兼容设置-->
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<!--360浏览器H5兼容设置-->
	<meta name="renderer" content="webkit" />
	<title>助农商城</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--导入核心文件-->
	<script src="../bootstrap3/js/holder.js"></script>
	<link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
	<script src="../bootstrap3/jquery-1.9.1.min.js"></script>
	<script src="../bootstrap3/js/bootstrap.js"></script>
	<!-- 字体图标 -->
	<link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css" />
	<link rel="stylesheet" type="text/css" href="../css/top.css" />
	<link rel="stylesheet" type="text/css" href="../css/index2.css" />
	<link rel="stylesheet" type="text/css" href="../css/layout.css" />
	<link rel="stylesheet" type="text/css" href="../css/reg.css" />
	<link rel="stylesheet" type="text/css" href="../css/footer.css" />
</head>
<body>
<!--头部-->
<header class="header">
	<img src="../images/index/stumalllogo.jpg" alt="" />
	<span class="pull-right"><small>欢迎访问</small><b>助农商城</b></span>
</header>
<!--主体-->
<div class="container mycontent text-left">
	<!--透明层 -->
	<div class="regDiv">
		新用户注册
		&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;
		&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;
		&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;
		&nbsp;	&nbsp;	&nbsp;

		<span class="errorMsg" style="color: #b92c28" ></span>


		<!--表单开始-->
		<form id="form-reg" class="form-horizontal" role="form">
			<!--用户名-->
			<div class="form-group">
				<label class="col-md-3 control-label">用户名：</label>
				<div class="col-md-8">
					<input id="username" name="username" type="text" class="form-control" placeholder="请输入用户名(长度为5-12位,由数字、密码、下划线组成)">
				</div>
			</div>
			<!-- 邮箱 -->
			<div class="form-group">
				<label class="col-md-3 control-label"> 邮箱：</label>
				<div class="col-md-8">
					<input id="email" name="email" type="text" class="form-control" placeholder="请输入邮箱">
				</div>
			</div>
			<!--密码-->
			<div class="form-group">
				<label class="col-md-3 control-label"> 密码：</label>
				<div class="col-md-8">
					<input id="password" name="password" type="text" class="form-control" placeholder="请输入密码(由数字、密码、下划线组成)">
				</div>
			</div>
			<!--提交按钮-->
			<div class="form-group">
				<label class="col-md-3 control-label"></label>
				<div class="col-md-8">
					<input id="btn-reg" class="btn btn-primary" type="button" value="立即注册" />
					<span class="pull-right"><small>已经有账号？</small><a href="login.html">登录</a></span>
				</div>
			</div>
		</form>
	</div>
</div>
<script type="text/javascript">
	$("#btn-reg").click(function () {
		//验证用户名
		let username = $("#username").val();
		var usernamePatt = /(?=.*[A-Za-z])(?=.*[\d])(?=.*_)/;
		var usernamePatt1 = /^\w{5,12}$/;
		if (!(usernamePatt.test(username) && usernamePatt1.test(username))) {
			$("span.errorMsg").text("用户名不合法！");
			return false;
		}


		// 验证邮箱：xxxxx@xxx.com
		let email = $("#email").val();
		var emailPatt = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/;
		//3 使用test方法验证是否合法
		if (!emailPatt.test(email)) {
			//4 提示用户
			$("span.errorMsg").text("邮箱格式不合法！");
			return false;
		}

		//验证密码
		let password = $("#password").val();
		var passwordPatt = /(?=.*[A-Za-z])(?=.*[\d])(?=.*_)/;
		var passwordPatt1 = /^\w{1,20}$/;


		if (!(passwordPatt.test(password) && passwordPatt1.test(password))) {
			$("span.errorMsg").text("密码不合法!");
			return false;
		}

		if (password.length<=6){
			$("span.errorMsg").text("密码强度过弱");
			return false;
		}


		$("span.errorMsg").text("");

		$.ajax({
			url:"/user/register",
			type:"POST",
			data:$("#form-reg").serialize(),
			dataType:"JSON",
			success:function (json) {
				if(json.state == 200){
					alert("注册成功");
					location.href = "/web/login.html";
				}else{
					alert("注册失败: "+json.message);
				}
			},
			error:function (xhr) {
				alert("未知错误"+xhr.status);
			}
		});
	});
</script>
</body>

</html>